//
//  JKRTopicTableViewCell.m
//  百思不得姐
//
//  Created by Lucky on 16/3/4.
//  Copyright © 2016年 Lucky. All rights reserved.
//

#import "JKRTopicTableViewCell.h"
#import "JKRTopic.h"
#import "JKRTopicPictureView.h"
#import "JKRTopicVoiceView.h"
#import "JKRTopicVideoView.h"

@interface JKRTopicTableViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *createTimeLabel;

@property (weak, nonatomic) IBOutlet UIButton *dingButton;

@property (weak, nonatomic) IBOutlet UIButton *caiButton;

@property (weak, nonatomic) IBOutlet UIButton *shareButton;

@property (weak, nonatomic) IBOutlet UIButton *commentButton;

@property (weak, nonatomic) IBOutlet UIImageView *sinaVView;

@property (weak, nonatomic) IBOutlet UILabel *text_label;

@property (strong, nonatomic) JKRTopicPictureView *pictureView;

@property (strong, nonatomic) JKRTopicVoiceView *voiceView;

@property (nonatomic, strong) JKRTopicVideoView *videoView;

@end

@implementation JKRTopicTableViewCell

- (void)awakeFromNib {
    
    UIImageView *bgView = [[UIImageView alloc] init];
    bgView.image = [UIImage imageNamed:@"mainCellBackground"];
    self.backgroundView = bgView;
}

- (void)setTopic:(JKRTopic *)topic
{
    _topic = topic;
    
    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:_topic.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    self.nameLabel.text = topic.name;
    self.createTimeLabel.text = topic.create_time;

    [self setupButtonTitle:self.dingButton count:topic.ding placehoder:@"顶"];
    [self setupButtonTitle:self.caiButton count:topic.cai placehoder:@"踩"];
    [self setupButtonTitle:self.shareButton count:topic.repost placehoder:@"分享"];
    [self setupButtonTitle:self.commentButton count:topic.comment placehoder:@"评论"];
    self.sinaVView.hidden = !topic.isSina_v;
    
    self.text_label.text = topic.text;
    
    if (topic.type == JKRTopicTypePicture) {
        self.pictureView.hidden = NO;
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
        
        self.pictureView.topic = topic;
        self.pictureView.frame = topic.pictureF;
    } else if (topic.type == JKRTopicTypeVoice) {
        self.voiceView.hidden = NO;
        self.pictureView.hidden = YES;
        self.videoView.hidden = YES;
        
        self.voiceView.frame = topic.voiceF;
        self.voiceView.topic = topic;
    } else if (topic.type == JKRTopicTypeVideo) {
        self.videoView.hidden = NO;
        self.voiceView.hidden = YES;
        self.pictureView.hidden = YES;
        
        self.videoView.frame = topic.videoF;
        self.videoView.topic = topic;
    } else if (topic.type) {
        self.videoView.hidden = YES;
        self.voiceView.hidden = YES;
        self.pictureView.hidden = YES;
    }
}

- (void)setupButtonTitle:(UIButton *)button count:(NSInteger)count placehoder:(NSString *)placehoder
{
    NSString *title = nil;
    if (count == 0) {
        title = placehoder;
    } else if (count > 10000) {
        title = [NSString stringWithFormat:@"%.lf万", count/10000.0];
    } else {
        title = [NSString stringWithFormat:@"%zd", count];
    }
    [button setTitle:title forState:UIControlStateNormal];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setFrame:(CGRect)frame
{
    static CGFloat margin = 10;
    
    frame.origin.x = margin;
    frame.size.width -= 2 * margin;
    frame.size.height -= margin;
    frame.origin.y += margin;
    
    [super setFrame:frame];
}

- (JKRTopicPictureView *)pictureView
{
    if (!_pictureView) {
        _pictureView = [JKRTopicPictureView pictureView];
        [self.contentView addSubview:_pictureView];
    }
    return _pictureView;
}

- (JKRTopicVoiceView *)voiceView
{
    if (!_voiceView) {
        _voiceView = [JKRTopicVoiceView voiceView];
        [self.contentView addSubview:_voiceView];
    }
    return _voiceView;
}

- (JKRTopicVideoView *)videoView
{
    if (!_videoView) {
        _videoView = [JKRTopicVideoView videoView];
        [self.contentView addSubview:_videoView];
    }
    return _videoView;
}

@end
